import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @作者 ： pwx5328113 @时间 ： 2023/2/27 @测试点 : rename table 兼容A库 兼容B库执行
 **/

public class Compatibility_Grammar_Renametable_Case0003 {
	public static void main(String[] args) throws Exception {
		if (args.length < 5) {
			System.out.print("need para: host:ip,db_a,db_b,user,password");
			return;
		}
		String url1 = "jdbc:postgresql://" + args[0] + "/" + args[1] + "?LoggerLevel=off";
		String url2 = "jdbc:postgresql://" + args[0] + "/" + args[2] + "?LoggerLevel=off";
		Connection conn_a = DriverManager.getConnection(url1, args[3], args[4]);
		Connection conn_b = DriverManager.getConnection(url2, args[3], args[4]);
		Statement stmt_a = null;
		Statement stmt_b = null;
		stmt_a = conn_a.createStatement();
		stmt_b = conn_b.createStatement();
		String err_msg = null;
		int suc_msg = -1;
		try {
			int rename_a = stmt_a.executeUpdate("create table test1(id int);rename table test1 to test");
			System.out.println("rename_a_result = " + rename_a);
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("rename_a_result = " + e.getMessage());
			err_msg = e.getMessage();
			System.out.println("drop_tb_result = " + stmt_a.executeUpdate("drop table if exists test1;"));
		}
		try {
			int rename_b = stmt_b.executeUpdate("create table test1(id int);rename table test1 to test");
			System.out.println("rename_b_result = " + rename_b);
			suc_msg = rename_b;
			stmt_b.execute("drop table if exists test1,test;");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		stmt_a.close();
		stmt_b.close();
		conn_a.close();
		conn_b.close();
		String err_flag = "ERROR: rename table syntax is supported on dbcompatibility B.";
		if (err_msg.indexOf(err_flag) != -1 && suc_msg == 0) {
			System.out.println("compatibility A database rename table returns error");
			System.out.println("compatibility B database rename table returns success");
			System.out.println("rename table success");
		}
	}

}
